import sys
import time
import json
from datetime import datetime
from selenium import webdriver
from selenium.common import exceptions
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import urllib.parse
from mongoengine import *
# import sendMessage


sys.path.append('.')
sys.path.append('..')
disconnect()
# connect('weibo', host='localhost', port=27017)


class locationuser(Document):
    nickname = StringField()
    uid = StringField()
    gender = IntField()
    followerCount = IntField()
    postCount = IntField()
    fansCount = IntField()
    city = StringField()
    addTime = DateTimeField()
    updatetime = DateTimeField()
    searched = IntField()
    location = StringField()
    weibourl = StringField()
    weiboDetail = StringField()
    category = IntField()
    weiboDate = DateTimeField()
    dateStr = StringField()

def NewDriver():
    chrome_options = Options()
    chrome_options.add_argument("user-data-dir=" + path2  + 'locationbyurl')
    # print('run',a,b)
    driver = webdriver.Chrome(path, options=chrome_options)
    # print('run2',a,b)
    driver.maximize_window()
    return driver


def gotolocaltion():
    # driver.execute_script('window.open()')
    # windows = driver.window_handles
    # driver.switch_to.window(windows[1])
    # local = driver.find_element_by_class_name('ficon_cd_place')
    # local.click()
    weibotext = driver.find_element_by_class_name('WB_text').find_element_by_tag_name('a')
    # print(weibotext.get_attribute('href'))
    driver.get(weibotext.get_attribute('href'))

def runuserlist():
    global url
    global location
    for index in range(0,26):
        time.sleep(1)
        driver.find_element_by_tag_name('body').send_keys(Keys.END)
    userList = driver.find_elements_by_class_name('WB_feed_detail')
    # [0].find_elements_by_class_name('list_li')
    # print(len(userList),'okok')
    for item in userList:
        try:
            detail = item.find_element_by_class_name('WB_text').get_attribute('textContent')
            weibodate =  item.find_element_by_class_name('WB_from').find_element_by_tag_name('a').get_attribute('title')
            # weibodate = datetime.datetime.strptime(weibodate, "%Y-%m-%d").date()
            
            # print(weibodate)
            infos = item.find_element_by_class_name('opt').get_attribute('action-data')
            infos = infos.split("&")
            uid = ""
            nickname = ""
            for info in infos:
                if info[0:3] == "uid":
                    uid = info[4:]
                if info[0:5] == "fnick":
                    nickname = info[6:]
                # print(uid,nickname,detail)
            # uid = item.find_element_by_class_name('WB_info').get_attribute('usercard')[3:]
            # nick = item.find_element_by_tag_name('img').get_attribute('alt')
            # sendMessage.sendMessage(uid,'test')
            oneUser = locationuser.objects(location = location,uid=uid)
            if len(oneUser) == 0:
                newdata = locationuser(    
                    nickname = nickname,
                    uid = uid,
                    addTime = datetime.now(),
                    updatetime = datetime.now(),
                    searched = 0,
                    location = location,
                    weibourl = url,
                    category = 0,
                    weiboDetail = detail,
                    dateStr = weibodate
                    # weiboDate = weibodate
                )
                newdata.save()
        except Exception as e:
            print(str(e),'error')
    try:
        time.sleep(2)
        try:
            driver.find_element_by_class_name('more_txt').click()
        except:
            driver.find_element_by_class_name('next').click()
        time.sleep(0.5)
        runuserlist()
        # if justStart < 10:
        #         driver.find_element_by_tag_name('body').send_keys(Keys.END)
        #         time.sleep(5)
        #         justStart = justStart + 1
        #         runuserlist()

        # else:
        #     time.sleep(2)
        #     try:
        #         driver.find_element_by_class_name('more_txt').click()
        #     except:
        #         driver.find_element_by_class_name('next').click()
        #     time.sleep(0.5)
        #     runuserlist()
        
    except:
        return


if __name__ == '__main__':
    # url = sys.argv[1] #name
    # location = 'TAXX'
    # print(a,b,c)
    items = [{"url":"https://weibo.com/p/100101B2094551D264ABF44798","location":"TAXX"},{"url":"https://weibo.com/p/100101B2094255DB6AA3FA4098","location":"SPACE PLUS"},{"url":"https://weibo.com/p/100101B2094655D26AAAFE489F","location":"M2 WANN"},{"url":"https://weibo.com/p/100101B209475DD46DABF44399","location":"LINX"},{"url":"https://weibo.com/p/100101B2094255D56BA3FD4899","location":"FIRST X"},{"url":"https://weibo.com/p/100101B2094553D76FAAFC469E","location":"MODU"},{"url":"https://weibo.com/p/100101B2094254DB6CA1F9459A","location":"LIGHT CLUB"},{"url":"https://weibo.com/p/100101B2094251D76FA1FE459E","location":"MASTER"},{"url":"https://weibo.com/p/100101B2094257D46EAAFA4093","location":"BEEHIVE"},{"url":"https://weibo.com/p/100101B2094256D56CA3F54198","location":"OWNER CIRCLE"},{"url":"https://weibo.com/p/100101B2094757D068A3FD409A","location":"上海迪士尼乐园"},{"url":"https://weibo.com/p/100101B2094655D264A4F8419E","location":"欢乐谷"},{"url":"https://weibo.com/p/100101B2094757D06FA1F9489B","location":"上海影视乐园"},{"url":"https://weibo.com/p/100101B2094655D269ABFD469B","location":"静安寺"},{"url":"https://weibo.com/p/100101B2094654DB6BA7FF439B","location":"长风公园"},{"url":"https://weibo.com/p/100101B2094654DB6AABFC409B","location":"大宁灵石公园"},{"url":"https://weibo.com/p/100101B2094655D36DA6FE409E","location":"陆家嘴"},{"url":"https://weibo.com/p/100101B2094654DB65A4FE4692","location":"上海新国际博览中心"},{"url":"https://weibo.com/p/100101B2094655D26FA0FB449C","location":"共青国家森林公园"},{"url":"https://weibo.com/p/100101B2094757D068A1FD4098","location":"上海浦东国际机场"},{"url":"https://weibo.com/p/100101B2094654DB6FA0F4499E","location":"上海野生动物园"},{"url":"https://weibo.com/p/100101B2094654DB64A3F44892","location":"朱家角古镇旅游区"},{"url":"https://weibo.com/p/100101B2094654DB6DA5F54692","location":"太阳岛旅游度假区"},{"url":"https://weibo.com/p/100101B2094757D069A6F84592","location":"西塘古镇景区"},{"url":"https://weibo.com/p/100101B2094757D068A2F4469D","location":"周庄古镇"},{"url":"https://weibo.com/p/100101B2094757D06FA2FF4793","location":"东平国家森林公园"},{"url":"https://weibo.com/p/100101B2094651D46CA6FD499B","location":"西湖风景名胜区"}]
    
    # items = [{"url":"https://weibo.com/p/100101B2094255D56CA1FD4992","location":"1192弄老上海风情街"}]
    # items = [{"url":"https://weibo.com/p/100101B2094551D264ABF44798","location":"TAXX"},{"url":"https://weibo.com/p/100101B2094255DB6AA3FA4098","location":"SPACE PLUS"},{"url":"https://weibo.com/p/100101B2094655D26AAAFE489F","location":"M2 WANN"},{"url":"https://weibo.com/p/100101B209475DD46DABF44399","location":"LINX"},{"url":"https://weibo.com/p/100101B2094255D56BA3FD4899","location":"FIRST X"},{"url":"https://weibo.com/p/100101B2094553D76FAAFC469E","location":"MODU"},{"url":"https://weibo.com/p/100101B2094254DB6CA1F9459A","location":"LIGHT CLUB"},{"url":"https://weibo.com/p/100101B2094251D76FA1FE459E","location":"MASTER"},{"url":"https://weibo.com/p/100101B2094257D46EAAFA4093","location":"BEEHIVE"},{"url":"https://weibo.com/p/100101B2094256D56CA3F54198","location":"OWNER CIRCLE"},{"url":"https://weibo.com/p/100101B2094757D068A3FD409A","location":"上海迪士尼乐园"},{"url":"https://weibo.com/p/100101B2094655D264A4F8419E","location":"欢乐谷"},{"url":"https://weibo.com/p/100101B2094757D06FA1F9489B","location":"上海影视乐园"},{"url":"https://weibo.com/p/100101B2094655D269ABFD469B","location":"静安寺"},{"url":"https://weibo.com/p/100101B2094654DB6BA7FF439B","location":"长风公园"},{"url":"https://weibo.com/p/100101B2094654DB6AABFC409B","location":"大宁灵石公园"},{"url":"https://weibo.com/p/100101B2094655D36DA6FE409E","location":"陆家嘴"},{"url":"https://weibo.com/p/100101B2094654DB65A4FE4692","location":"上海新国际博览中心"},{"url":"https://weibo.com/p/100101B2094655D26FA0FB449C","location":"共青国家森林公园"},{"url":"https://weibo.com/p/100101B2094757D068A1FD4098","location":"上海浦东国际机场"},{"url":"https://weibo.com/p/100101B2094654DB6FA0F4499E","location":"上海野生动物园"},{"url":"https://weibo.com/p/100101B2094654DB64A3F44892","location":"朱家角古镇旅游区"},{"url":"https://weibo.com/p/100101B2094654DB6DA5F54692","location":"太阳岛旅游度假区"},{"url":"https://weibo.com/p/100101B2094757D069A6F84592","location":"西塘古镇景区"},{"url":"https://weibo.com/p/100101B2094757D068A2F4469D","location":"周庄古镇"},{"url":"https://weibo.com/p/100101B2094757D06FA2FF4793","location":"东平国家森林公园"},{"url":"https://weibo.com/p/100101B2094651D46CA6FD499B","location":"西湖风景名胜区"}]
    # items = [{"url":"https://weibo.com/p/100101B2094757D068A3FD409A","location":"上海迪士尼乐园"},{"url":"https://weibo.com/p/100101B2094655D264A4F8419E","location":"欢乐谷"},{"url":"https://weibo.com/p/100101B2094757D06FA1F9489B","location":"上海影视乐园"},{"url":"https://weibo.com/p/100101B2094655D269ABFD469B","location":"静安寺"},{"url":"https://weibo.com/p/100101B2094654DB6BA7FF439B","location":"长风公园"},{"url":"https://weibo.com/p/100101B2094654DB6AABFC409B","location":"大宁灵石公园"},{"url":"https://weibo.com/p/100101B2094655D36DA6FE409E","location":"陆家嘴"},{"url":"https://weibo.com/p/100101B2094654DB65A4FE4692","location":"上海新国际博览中心"},{"url":"https://weibo.com/p/100101B2094655D26FA0FB449C","location":"共青国家森林公园"},{"url":"https://weibo.com/p/100101B2094757D068A1FD4098","location":"上海浦东国际机场"},{"url":"https://weibo.com/p/100101B2094654DB6FA0F4499E","location":"上海野生动物园"},{"url":"https://weibo.com/p/100101B2094654DB64A3F44892","location":"朱家角古镇旅游区"},{"url":"https://weibo.com/p/100101B2094654DB6DA5F54692","location":"太阳岛旅游度假区"},{"url":"https://weibo.com/p/100101B2094757D069A6F84592","location":"西塘古镇景区"},{"url":"https://weibo.com/p/100101B2094757D068A2F4469D","location":"周庄古镇"},{"url":"https://weibo.com/p/100101B2094757D06FA2FF4793","location":"东平国家森林公园"},{"url":"https://weibo.com/p/100101B2094651D46CA6FD499B","location":"西湖风景名胜区"}]
    # items = [{"url":"https://weibo.com/p/100101B2094255D56CA1FD4992","location":"1192弄老上海风情街"},{"url":"https://weibo.com/p/100101B2094256D465A5FA469C","location":"CORNER CONE GELATO"},{"url":"https://weibo.com/p/100101B2094255D56CA1FD4992","location":"Pie Bird"},{"url":"https://weibo.com/p/100101B2094254D669A6F84198","location":"%Arabica烘焙工坊"},{"url":"https://weibo.com/p/100101B2094256D464A4FF4998","location":"多云书院旗舰店"},{"url":"https://weibo.com/p/100101B2094551D76FA0FD449F","location":"哥伦比亚公园"},{"url":"https://weibo.com/p/100101B2094254D56BABFC4799","location":"顶层酒吧ROOF"},{"url":"https://weibo.com/p/100101B2094251D065A1F5449A","location":"RIINK"},{"url":"https://weibo.com/p/100101B2094556D06BA0FC4293","location":"Highline"},
    items = [{"url":"https://weibo.com/p/100101B2094556D06BA0FC4293","location":"鸢屋书店"},{"url":"https://weibo.com/p/100101B2094551DA6BAAF84193","location":"BAR FLOW"},{"url":"https://weibo.com/p/100101B2094556D06BA0FC4293","location":"The Odd Couple"},{"url":"https://weibo.com/p/100101B2094257D069A3FD419D","location":"The Odd Couple"},{"url":"https://weibo.com/p/100101B2094257D565A1F8439B","location":"PARTY KING 运动街区"},{"url":"https://weibo.com/p/100101B2094257DA6EAAFE4099","location":"REVEL"},{"url":"https://weibo.com/p/100101B2094254D665A4FD439C","location":"CYPHER"},{"url":"https://weibo.com/p/100101B2094551D56CA3FC4699","location":"Bar lotus"},{"url":"https://weibo.com/p/100101B2094256D664A7F9469E","location":"44KW"},{"url":"https://weibo.com/p/100101B2094753D36CA0FD409B","location":"罗斯福色戒酒吧"},{"url":"https://weibo.com/p/100101B2094556D06BA0FC4293","location":"Donutzwoo"},{"url":"https://weibo.com/p/100101B2094257D768A5F4469C","location":"83BANDS CLUB"},{"url":"https://weibo.com/p/100101B2094256D36FAAF4459F","location":"抱朴美术馆"},{"url":"https://weibo.com/p/100101B2094550D66FA1FE439F","location":"上海油罐艺术中心"},{"url":"https://weibo.com/p/100101B2094255DB6AA5FF4393","location":"玻璃宫艺术书局"},{"url":"https://weibo.com/p/100101B2094254DA6BA3F44393","location":"志屋"},{"url":"https://weibo.com/p/100101B2094257DB65ABFB449C","location":"思南书局诗歌店"},{"url":"https://weibo.com/p/100101B2094257D46CA3FC409C","location":"绿瓦体育书店"},{"url":"https://weibo.com/p/100101B2094250D26BA3FD469E","location":"幸福集荟"},{"url":"https://weibo.com/p/100101B2094551D064A2FD429A","location":"光的空间新华书店"},{"url":"https://weibo.com/p/100101B2094251D565A3FA4892","location":"BARBARIAN"},{"url":"https://weibo.com/p/100101B2094750D164ABFF4693","location":"seesaw"},{"url":"https://weibo.com/p/100101B2094251D56DA5FE459C","location":"HINICHIJOU"},{"url":"https://weibo.com/p/100101B2094251D16AA3F8469F","location":"Hugme Cafe"},{"url":"https://weibo.com/p/100101B2094551DB65A5FF4698","location":"彩虹昔"},{"url":"https://weibo.com/p/100101B2094551D16FA5FF4399","location":"RAC BAR"},{"url":"https://weibo.com/p/100101B2094255D56DABFD419A","location":"小小花园"},{"url":"https://weibo.com/p/100101B2094256D76EABF8409A","location":"话梅"}]
    index =0
    mode = 1
    path = 'd:/work/weiboposition/python/chromedriver.exe'
    path2 = 'd:/work/weiboposition/profile/'
    if mode == 1:
        path = 'd:/weiboAdmin/python/chromedriver.exe'
        path2 = 'd:/weiboAdmin/profile/'
        #connect('weibo', host='192.168.1.176', port=27017)
        connect('weibo', host='localhost', port=27017)
    
    
    else:
        connect('weibo', host='localhost', port=27017)
    driver = NewDriver()
    driver.get("https://weibo.com")
    time.sleep(3)

    while True:
        try:
            item = items[index]
            url = item["url"]
            location = item["location"]
            print(url,'url')
            driver.get(url)
            time.sleep(3)

        
            gotolocaltion()
            time.sleep(3)
            runuserlist()
            index = index + 1
        except:
            index = index + 1



    # while True:
    #     try:
    #         time.sleep(3)
    #         runuserlist()
    #     except:
    #         time.sleep(3)
    #         runuserlist()

    # while True:
    #     try:
    #         url = "https://weibo.com/{Uid}/{URL}?type=repost"
    #         print(url.format(Uid = uid,URL=repostID),'url')
    #         driver.get(url.format(Uid = uid,URL=repostID))
    #         # url = 'https://weibo.com/2042697783/IwTjWarB4?type=repost'
    #         # driver.get(url)
    #         time.sleep(3)
        # driver.execute_script('window.open()')
        # # driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
        # windows = driver.window_handles
        # driver.switch_to.window(windows[1])
        # driver.switch_to.window(windows[0])
        # time.sleep(1)
    #         try:
    #             getRepostUserList()
    #         except:
    #             getRepostUserList()
    #     except:
    #         # fansUrl = "https://weibo.com/{UID}/fans?cfs=600&relate=fans&t=1&f=1&type=&Pl_Official_RelationFans__90_page={page}#Pl_Official_RelationFans__90"
    #         # driver.get(fansUrl.format(UID=uid, page=1))
    #         # getFans(driver,uid,account)
    #         continue
